package com.example.myapplication.ui.news;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;

import com.example.myapplication.R;
import com.example.myapplication.base.BaseMvpActivity;
import com.example.myapplication.model.bean.NewsChannelTable;
import com.example.myapplication.ui.news.NewsPresenter.NewChannelContract;
import com.example.myapplication.ui.news.NewsPresenter.NewChannelPresenter;
import com.example.myapplication.ui.news.adapter.ChannelAdapter;
import com.example.myapplication.ui.wight.ItemDragHelperCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class NewsChannelActivity extends BaseMvpActivity<NewChannelPresenter> implements NewChannelContract.View {

    private int fromPosition;
    private int toPosition;

    public  static  void startAction(Context context){
        Intent intent =new Intent(context,NewsChannelActivity.class);
        context.startActivity(intent);

    }

    @Override
    protected NewChannelPresenter initPresenter() {
        return new NewChannelPresenter(this);
    }

    @BindView(R.id.news_channel_mine_rv)
    RecyclerView news_channel_mine_rv;
    @BindView(R.id.news_channel_more_rv)
   RecyclerView news_channel_more_rv;

    ChannelAdapter adapter;
    ChannelAdapter adaptermore;
    @Override
    protected int getLayout() {
        return R.layout.activity_news_channel;
    }

    @Override
    protected void initView() {
        news_channel_mine_rv.setLayoutManager(new GridLayoutManager(this,4,LinearLayoutManager.VERTICAL,false));
        adapter =new ChannelAdapter(R.layout.item_news_channel,new ArrayList<>());
        news_channel_mine_rv.setAdapter(adapter);

        news_channel_more_rv.setLayoutManager(new GridLayoutManager(this, 4, LinearLayoutManager.VERTICAL, false));
        adaptermore =new ChannelAdapter(R.layout.item_news_channel,new ArrayList<>());
        news_channel_more_rv.setAdapter(adaptermore);

        adapter.setOnRecyclerViewItemClickListener((view, position) -> {

            NewsChannelTable  newsChannel =adapter.getData().get(position);
            List<NewsChannelTable> newL=new ArrayList<NewsChannelTable>();
                 newL.add(newsChannel);
            adaptermore.addData(newL);
            adapter.remove(position);
            mPresenter.requestDataMoreNewsChannels((ArrayList<NewsChannelTable>) adapter.getData(), (ArrayList<NewsChannelTable>) adaptermore.getData());
        });
        ItemDragHelperCallback itemDragHelperCallback = new ItemDragHelperCallback(adapter);
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemDragHelperCallback);

        itemTouchHelper.attachToRecyclerView(news_channel_more_rv);
        adapter.setItemDragHelperCallback(itemDragHelperCallback);
        adaptermore.setOnRecyclerViewItemClickListener((view, position) -> {

            NewsChannelTable newsChannel = adaptermore.getData().get(position);
            List<NewsChannelTable> newL=new ArrayList<NewsChannelTable>();
            newL.add(newsChannel);
            adapter.addData(newL);
            adaptermore.remove(position);
           mPresenter.requestDataMoreNewsupata((ArrayList<NewsChannelTable>) adapter.getData(), (ArrayList<NewsChannelTable>)adaptermore.getData());
        });
        mPresenter.requestDataMineNewsChannels();

    }
    @Override
    public void showError(String msg) {

    }

    @Override
    public void returnMineNewsChannels(List<NewsChannelTable> newsChannelsMine) {
            adapter.addData(newsChannelsMine);
    }

    @Override
    public void returnMoreNewsChannels(List<NewsChannelTable> newsChannelsMore) {
        adaptermore.addData(newsChannelsMore);
    }

}
